 /***************************************************************************
 * Copyright (C) 2012
 * by Dj_Skual
 *
 * This software is provided 'as-is', without any express or implied
 * warranty. In no event will the authors be held liable for any
 * damages arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any
 * purpose, including commercial applications, and to alter it and
 * redistribute it freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you
 * must not claim that you wrote the original software. If you use
 * this software in a product, an acknowledgment in the product
 * documentation would be appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and
 * must not be misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source
 * distribution.
 *
 * TaskBar.cpp
 *
 * for SaveGame Manager GX 2012
 ***************************************************************************/
#include "TaskBar.hpp"
#include "Explorer.h"
#include "../Themes/CTheme.h"
#include "../Tools/PasswordCheck.h"
#include "../Controls/Application.h"
#include "../Prompts/SearchWindow.h"
#include "../Prompts/PromptWindows.h"

TaskBar *TaskBar::instance = NULL;

TaskBar::TaskBar()
	: GuiFrame(0, 0)
{
	button.clear();
	tooltip.clear();
	img.clear();
	imgGray.clear();
	data.clear();
	dataGray.clear();
	
	btnSoundClick = Resources::GetSound("button_click.wav");
	btnSoundOver = Resources::GetSound("button_over.wav");
	btnSoundClick->SetVolume(Settings.SFXVolume);
	btnSoundOver->SetVolume(Settings.SFXVolume);
	
	trigA.SetSimpleTrigger(-1, WiiControls.ClickButton | ClassicControls.ClickButton << 16, GCControls.ClickButton);
	
	data.push_back(Resources::GetImageData("taskbar_sd.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_sd_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_usb.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_usb_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_wii.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_wii_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_emu.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_emu_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_mii.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_mii_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_emu_mii.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_emu_mii_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_gca.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_gca_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_gcb.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_gcb_gray.png"));
	
	data.push_back(Resources::GetImageData("taskbar_arrangeIcon.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_arrangeIcon_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_arrangeList.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_arrangeList_gray.png"));
	
	data.push_back(Resources::GetImageData("taskbar_locked.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_locked_gray.png"));
	data.push_back(Resources::GetImageData("taskbar_unlocked.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_unlocked_gray.png"));
	
	data.push_back(Resources::GetImageData("taskbar_search.png"));
	dataGray.push_back(Resources::GetImageData("taskbar_search_gray.png"));
	
	img.push_back(new GuiImage(data[TASKBAR_SD]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_SD]));
	img.push_back(new GuiImage(data[TASKBAR_USB]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_USB]));
	img.push_back(new GuiImage(data[TASKBAR_WII]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_WII]));
	img.push_back(new GuiImage(data[TASKBAR_EWII]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_EWII]));
	img.push_back(new GuiImage(data[TASKBAR_MII]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_MII]));
	img.push_back(new GuiImage(data[TASKBAR_EMII]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_EMII]));
	img.push_back(new GuiImage(data[TASKBAR_GCA]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_GCA]));
	img.push_back(new GuiImage(data[TASKBAR_GCB]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_GCB]));
	
	img.push_back(new GuiImage(data[TASKBAR_ICON]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_ICON]));
	img.push_back(new GuiImage(data[TASKBAR_LIST]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_LIST]));
	
	img.push_back(new GuiImage(data[TASKBAR_LOCKED]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_LOCKED]));
	img.push_back(new GuiImage(data[TASKBAR_UNLOCKED]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_UNLOCKED]));
	
	img.push_back(new GuiImage(data[TASKBAR_SEARCH]));
	imgGray.push_back(new GuiImage(dataGray[TASKBAR_SEARCH]));
	
	tooltip.push_back(new GuiTooltip(tr( "Select Device" )));
	tooltip[BTN_DEVICE]->SetAlignment(thAlign("left - taskbar device tooltip align hor") | thAlign("bottom - taskbar device tooltip align ver"));
	tooltip[BTN_DEVICE]->SetPosition(thInt("40 - taskbar device tooltip pos x"), thInt("35 - taskbar device tooltip pos y"));
	tooltip[BTN_DEVICE]->SetAlpha(thInt("255 - tooltip alpha"));
	
	tooltip.push_back(new GuiTooltip(tr( "Display Mode" )));
	tooltip[BTN_DISPLAY]->SetAlignment(thAlign("left - taskbar arrange tooltip align hor") | thAlign("bottom - taskbar arrange tooltip align ver"));
	tooltip[BTN_DISPLAY]->SetPosition(thInt("40 - taskbar arrange tooltip pos x"), thInt("35 - taskbar arrange tooltip pos y"));
	tooltip[BTN_DISPLAY]->SetAlpha(thInt("255 - tooltip alpha"));
	
	tooltip.push_back(new GuiTooltip(Settings.GodMode ? tr( "Lock Console" ) : tr( "Unlock Console" )));
	tooltip[BTN_LOCK]->SetAlignment(thAlign("right - taskbar lock tooltip align hor") | thAlign("bottom - taskbar lock tooltip align ver"));
	tooltip[BTN_LOCK]->SetPosition(thInt("-40 - taskbar lock tooltip pos x"), thInt("35 - taskbar lock tooltip pos y"));
	tooltip[BTN_LOCK]->SetAlpha(thInt("255 - tooltip alpha"));
	
	tooltip.push_back(new GuiTooltip(tr( "Search" )));
	tooltip[BTN_SEARCH]->SetAlignment(thAlign("right - taskbar search tooltip align hor") | thAlign("bottom - taskbar search tooltip align ver"));
	tooltip[BTN_SEARCH]->SetPosition(thInt("-40 - taskbar search tooltip pos x"), thInt("35 - taskbar search tooltip pos y"));
	tooltip[BTN_SEARCH]->SetAlpha(thInt("255 - tooltip alpha"));
	
	button.push_back(new PictureButton("taskbar_sd_gray.png", "taskbar_sd.png", btnSoundClick, btnSoundOver));
	button[BTN_DEVICE]->SetAlignment(thAlign("left - taskbar device button align hor") | thAlign("top - taskbar device button align ver"));
	button[BTN_DEVICE]->SetPosition(thInt("0 - taskbar device button pos x"), thInt("0 - taskbar device button pos y"));
	button[BTN_DEVICE]->SetTooltip(tooltip[BTN_DEVICE]);
	button[BTN_DEVICE]->SetTrigger(&trigA);
	button[BTN_DEVICE]->Clicked.connect(this, &TaskBar::OnDeviceButtonClick);
	button[BTN_DEVICE]->StateChanged.connect(this, &TaskBar::OnButtonStateChange);
	Append(button[BTN_DEVICE]);
	
	button.push_back(new PictureButton("taskbar_arrangeIcon_gray.png", "taskbar_arrangeIcon.png", btnSoundClick, btnSoundOver));
	button[BTN_DISPLAY]->SetAlignment(thAlign("left - taskbar arrange button align hor") | thAlign("top - taskbar arrange button align ver"));
	button[BTN_DISPLAY]->SetPosition(thInt("40 - taskbar arrange button pos x"), thInt("0 - taskbar arrange button pos y"));
	button[BTN_DISPLAY]->SetTooltip(tooltip[BTN_DISPLAY]);
	button[BTN_DISPLAY]->SetTrigger(&trigA);
	button[BTN_DISPLAY]->Clicked.connect(this, &TaskBar::OnArrangeButtonClick);
	button[BTN_DISPLAY]->StateChanged.connect(this, &TaskBar::OnButtonStateChange);
	Append(button[BTN_DISPLAY]);
	
	button.push_back(new PictureButton("taskbar_locked_gray.png", "taskbar_locked.png", btnSoundClick, btnSoundOver));
	button[BTN_LOCK]->SetAlignment(thAlign("left - taskbar lock button align hor") | thAlign("top - taskbar lock button align ver"));
	button[BTN_LOCK]->SetPosition(thInt("80 - taskbar lock button pos x"), thInt("0 - taskbar lock button pos y"));
	button[BTN_LOCK]->SetTooltip(tooltip[BTN_LOCK]);
	button[BTN_LOCK]->SetTrigger(&trigA);
	button[BTN_LOCK]->Clicked.connect(this, &TaskBar::OnLockButtonClick);
	button[BTN_LOCK]->StateChanged.connect(this, &TaskBar::OnButtonStateChange);
	Append(button[BTN_LOCK]);
	
	button.push_back(new PictureButton("taskbar_search_gray.png", "taskbar_search.png", btnSoundClick, btnSoundOver));
	button[BTN_SEARCH]->SetAlignment(thAlign("left - taskbar search button align hor") | thAlign("top - taskbar search button align ver"));
	button[BTN_SEARCH]->SetPosition(thInt("120 - taskbar search button pos x"), thInt("0 - taskbar search button pos y"));
	button[BTN_SEARCH]->SetTooltip(tooltip[BTN_SEARCH]);
	button[BTN_SEARCH]->SetTrigger(&trigA);
	button[BTN_SEARCH]->Clicked.connect(this, &TaskBar::OnSearchButtonClick);
	button[BTN_SEARCH]->StateChanged.connect(this, &TaskBar::OnButtonStateChange);
	Append(button[BTN_SEARCH]);
	
	UpdateButtons();
	UpdateSearchButton();
	
	SetSize(thInt("160 - taskbar size x"), thInt("32 - taskbar size y"));
	SetAlignment(thAlign("center - taskbar align hor") | thAlign("top - taskbar align ver"));
	SetPosition(thInt("0 - taskbar pos x"), thInt("13 - taskbar pos y"));
	
	pointed = new GuiPointed(width, height);
	pointed->StateChanged.connect(this, &TaskBar::OnStateChange);
	Append(pointed);
}

TaskBar::~TaskBar()
{
	RemoveAll();
	
	for(int i = 0; i < (int)button.size(); ++i)
	{
		delete button[i];
		delete tooltip[i];
	}
	button.clear();
	tooltip.clear();
	
	for(int i = 0; i < (int)img.size(); ++i)
	{
		delete img[i];
		delete imgGray[i];
		Resources::Remove(data[i]);
		Resources::Remove(dataGray[i]);
	}
	img.clear();
	imgGray.clear();
	data.clear();
	dataGray.clear();
	
	delete pointed;
	
	Resources::Remove(btnSoundClick);
	Resources::Remove(btnSoundOver);
}

void TaskBar::show()
{
	SetEffect(EFFECT_FADE, 20);
	Application::Instance()->Append(this);
}

void TaskBar::hide()
{
	SetEffect(EFFECT_FADE, -20);
}

void TaskBar::UpdateButtons()
{
	//!Device Button
	if(Settings.LoadedBrowser == SDUSB_BROWSER)
	{
		std::string dev = Settings.LastUsedPath;
		dev = dev.substr(0, dev.find_first_of(":"));
		
		if(!dev.compare("sd"))
		{
			button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_SD]);
			button[BTN_DEVICE]->SetImageOver(img[TASKBAR_SD]);
		}
		else
		{
			button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_USB]);
			button[BTN_DEVICE]->SetImageOver(img[TASKBAR_USB]);
		}
	}
	else if(Settings.LoadedBrowser == SAVE_BROWSER)
	{
		button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_WII]);
		button[BTN_DEVICE]->SetImageOver(img[TASKBAR_WII]);
	}
	else if(Settings.LoadedBrowser == ESAVE_BROWSER)
	{
		button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_EWII]);
		button[BTN_DEVICE]->SetImageOver(img[TASKBAR_EWII]);
	}
	else if(Settings.LoadedBrowser == MII_BROWSER)
	{
		button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_MII]);
		button[BTN_DEVICE]->SetImageOver(img[TASKBAR_MII]);
	}
	else if(Settings.LoadedBrowser == EMII_BROWSER)
	{
		button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_EMII]);
		button[BTN_DEVICE]->SetImageOver(img[TASKBAR_EMII]);
	}
	else if(Settings.LoadedBrowser == GC_BROWSER)
	{
		if(!Settings.LastUsedPath.compare("gca"))
		{
			button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_GCA]);
			button[BTN_DEVICE]->SetImageOver(img[TASKBAR_GCA]);
		}
		else
		{
			button[BTN_DEVICE]->SetImage(imgGray[TASKBAR_GCB]);
			button[BTN_DEVICE]->SetImageOver(img[TASKBAR_GCB]);
		}
	}
	
	//!Display Button
	if(Settings.BrowserDisplayMode == ICON_DISPLAY)
	{
		button[BTN_DISPLAY]->SetImage(imgGray[TASKBAR_ICON]);
		button[BTN_DISPLAY]->SetImageOver(img[TASKBAR_ICON]);
	}
	else
	{
		button[BTN_DISPLAY]->SetImage(imgGray[TASKBAR_LIST]);
		button[BTN_DISPLAY]->SetImageOver(img[TASKBAR_LIST]);
	}
	
	//!Lock Button
	if(Settings.GodMode)
	{
		tooltip[BTN_LOCK]->SetText( tr("Lock Console") );
		button[BTN_LOCK]->SetImage(imgGray[TASKBAR_UNLOCKED]);
		button[BTN_LOCK]->SetImageOver(img[TASKBAR_UNLOCKED]);
	}
	else
	{
		tooltip[BTN_LOCK]->SetText( tr("Unlock Console") );
		button[BTN_LOCK]->SetImage(imgGray[TASKBAR_LOCKED]);
		button[BTN_LOCK]->SetImageOver(img[TASKBAR_LOCKED]);
	}
}

void TaskBar::UpdateSearchButton()
{
	if (!CurrentBrowser()->GetCurrentFilter().empty() &&
		!SearchWindow::Instance()->IsRunning())
	{
		button[BTN_SEARCH]->SetEffect(EFFECT_PULSE, 5, 105);
		button[BTN_SEARCH]->SetImage(img[TASKBAR_SEARCH]);
		button[BTN_SEARCH]->SetImageOver(img[TASKBAR_SEARCH]);
	}
	else if(!SearchWindow::Instance()->IsRunning())
	{
		button[BTN_SEARCH]->ResetEffects();
		button[BTN_SEARCH]->SetImage(imgGray[TASKBAR_SEARCH]);
		button[BTN_SEARCH]->SetImageOver(img[TASKBAR_SEARCH]);
		button[BTN_SEARCH]->SetEffectGrow();
	}
	else
	{
		button[BTN_SEARCH]->ResetEffects();
		button[BTN_SEARCH]->SetImage(img[TASKBAR_SEARCH]);
		button[BTN_SEARCH]->SetImageOver(imgGray[TASKBAR_SEARCH]);
		button[BTN_SEARCH]->SetEffectGrow();
	}
}

void TaskBar::UpdateAllButtons()
{
	UpdateButtons();
	UpdateSearchButton();
}

void TaskBar::OnStateChange(GuiElement *sender UNUSED, int State, int stateChan UNUSED)
{
	if(State == STATE_SELECTED)
		Application::Instance()->BringToFront(this);
}

void TaskBar::OnButtonStateChange(GuiElement *sender, int State, int stateChan UNUSED)
{
	if(State == STATE_SELECTED)
		BringToFront(sender);
}

void TaskBar::OnDeviceButtonClick(GuiButton *sender, int pointer UNUSED, const POINT &p UNUSED)
{
	sender->SetState(STATE_DISABLED);
	Application::Instance()->updateEvents();
	
	DeviceMenu * deviceMenu = new DeviceMenu();
	deviceMenu->Clicked.connect(this, &TaskBar::OnDeviceMenuClick);
}

void TaskBar::OnDeviceMenuClick(DeviceMenu * deviceMenu, int dev)
{
	deviceMenu->SetEffect(EFFECT_FADE, -20);
	deviceMenu->Close();
	
	if(dev >= 0)
	{
		if(dev <= USB9)
		{
			Settings.LoadedBrowser = SDUSB_BROWSER;
			Settings.LastUsedPath = DeviceName[dev];
			Settings.LastUsedPath += ":/";
		}
		else if(dev == WII)
			Settings.LoadedBrowser = SAVE_BROWSER;
		else if(dev == MII)
			Settings.LoadedBrowser = MII_BROWSER;
		else if(dev >= EWII1 && dev <= EMII3)
		{
			if(dev <= EWII3)
				Settings.LoadedBrowser = ESAVE_BROWSER;
			else
				Settings.LoadedBrowser = EMII_BROWSER;
			
			if(dev == EWII1 || dev == EMII1)
				Settings.EmuUsedPath = Settings.EmuNandPath_1;
			else if(dev == EWII2 || dev == EMII2)
				Settings.EmuUsedPath = Settings.EmuNandPath_2;
			else if(dev == EWII3 || dev == EMII3)
				Settings.EmuUsedPath = Settings.EmuNandPath_3;
			
		}
		else if(dev == GCA || dev == GCB)
		{
			Settings.LoadedBrowser = GC_BROWSER;
			Settings.GCUsedPath = (dev == GCA ? "gca" : "gcb");
		}
		
		Explorer::Instance()->Reload();
		CurrentBrowser()->FilterList();
		
		if(SearchWindow::Instance()->IsRunning())
			SearchWindow::Instance()->Refresh();
	}
	
	button[BTN_DEVICE]->SetState(STATE_DEFAULT);
	
	UpdateAllButtons();
}

void TaskBar::OnArrangeButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	Application::Instance()->ResetPads();
	
	Settings.BrowserDisplayMode = !Settings.BrowserDisplayMode;
	
	Explorer::Instance()->SwitchDisplayMode();
	
	if(SearchWindow::Instance()->IsRunning())
		SearchWindow::Instance()->Refresh();
	
	UpdateButtons();
}

void TaskBar::OnLockButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	if (!Settings.GodMode)
	{
		//!password check to unlock
		int result = PasswordCheck(Settings.unlockCode);
		if (result > 0)
		{
			if(result == 1)
				WindowPrompt( tr( "Correct Password" ), tr( "All the features of SaveGame Manager GX are unlocked." ), tr( "OK" ));
			Settings.GodMode = ON;
		}
		else if(result < 0)
			WindowPrompt(tr( "Wrong Password" ), tr( "SaveGame Manager GX is protected" ), tr( "OK" ));
	}
	else if (WindowPrompt(tr( "Lock Console" ), tr( "Are you sure?" ), tr( "Yes" ), tr( "No" )))
	{
		WindowPrompt(tr( "Console Locked" ), tr( "SaveGame Manager GX is protected" ), tr( "OK" ));
		Settings.GodMode = OFF;
	}
	
	UpdateButtons();
}

void TaskBar::OnSearchButtonClick(GuiButton *sender UNUSED, int pointer UNUSED, const POINT &p UNUSED)
{
	Application::Instance()->ResetPads();
	
	if (!CurrentBrowser()->GetCurrentFilter().size() &&
		!SearchWindow::Instance()->IsRunning())
		CurrentBrowser()->FilterList();
	
	if (!SearchWindow::Instance()->IsRunning())
		SearchWindow::Instance()->Show();
	else
		SearchWindow::Instance()->Hide();
	
	UpdateSearchButton();
}
